home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8884 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: access1.digex.net!not-for-mail
  2. From: ell@access1.digex.net (Ell)
  3. Newsgroups: comp.object,comp.lang.c++
  4. Subject: Re: A design question
  5. Followup-To: comp.object,comp.lang.c++
  6. Date: 27 Feb 1996 05:20:59 GMT
  7. Organization: The Universe
  8. Message-ID: <4gu4br$bmc@news4.digex.net>
  9. References: <1996Feb22.234825.18755@dcs.warwick.ac.uk>
  10. NNTP-Posting-Host: access1.digex.net
  11. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  12.  
  13. Robert C. Martin (rmartin@oma.com) wrote:
  14. :    > miro@dcs.warwick.ac.uk (Miroslav J Walker) writes:
  15. :    >    I'm trying to model input from a user being processed over a
  16. :    >    series of steps.  Input is parsed, put into a queue and then
  17. :    >    interpreted on a timer. My question is how to model the idea
  18. :    >    of it being the same bit of data (essentially) taking on
  19. :    >    several forms as it proceeds along the 'pipeline' of
  20. :    >    processing.
  21. :    > 
  22.  
  23. :    Richard Berman <sts@crl.com> writes:
  24. :    >>    Of course, a state table might do the trick as well.
  25.   
  26. : Indeed.  And there is a very nice pattern in the "Design Patterns"
  27. : book that describes the way in which a state machine can be
  28. : implemented to do just what Miroslav is asking.  The pattern is called
  29. : "State".  
  30. : Consider the following:
  31. : class MyParcel;  /fwd declare.
  32. : // this class is the abstract representation of the state of the
  33. : // parcel of data.  It defines functions which respond to all the
  34. : // events that a parcel may experience.
  35. : class ParcelState
  36. : {
  37. :   public:
  38. :     // the events that occur along the processing chain.
  39. :     virtual void Event1(MyParcel&) = 0;
  40. :     virtual void Event2(MyParcel&) = 0;
  41. : };
  42. : // This is the parcel itself.  It also defines functions which respond
  43. : // to all its events.  However, these functions simply delegate to the
  44. : // contained state object.
  45. : class MyParcel
  46. : {
  47. :   public:
  48. :     MyParcel();
  49. :     void Event1() {itsState->Event1(*this);}
  50. :     void Event2() {itsState->Event2(*this);}
  51. :   private:
  52. :     ParcelState* itsState;
  53. : };
  54.  
  55. I'm wondering if the above class MyParcel shouldn't publicly inherit from 
  56. ParcelState?
  57.  
  58. Elliott
  59.